﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class SimpleMenuItem : WebControl
{
    private const string BRANCH = "branch";

    string _url;
    string _title;
    string _cssClass;
    public List<SimpleMenuItem> MenuItems { get; set; }

    public SimpleMenuItem(SiteMapNode node, string cssClass)
    {
        _url = node.Url;
        _title = node.Title;
        _cssClass = cssClass;
        MenuItems = new List<SimpleMenuItem>();
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (!string.IsNullOrEmpty(_cssClass))
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, _cssClass);
        }
        writer.RenderBeginTag(HtmlTextWriterTag.Li);
        if (MenuItems.Count > 0)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, BRANCH);
        }
        writer.AddAttribute(HtmlTextWriterAttribute.Href, _url);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
        
        writer.Write(_title);
            writer.RenderEndTag(); // a

        if (MenuItems.Count > 0)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);

            foreach (SimpleMenuItem child in MenuItems)
            {
                child.Render(writer);
            }

            writer.RenderEndTag(); // ul
        }
        writer.RenderEndTag(); // li

    }
}